import 'dart:async';
import 'package:bloc/bloc.dart';
import './bloc.dart';
import 'package:drm/base/login_result_entity.dart';

class LoginBloc extends Bloc<LoginEvent, LoginState> {
  @override
  LoginState get initialState => InitialLoginState();

  @override
  Stream<LoginState> mapEventToState(
    LoginEvent event,
  ) async* {
    if (event is LoadLoaclUser) {
      print('读取本地user数据');
      event.getLocalUser();
      yield LoadLoaclUserState(event.user);
    }

    if (event is Login) {

      yield LoginLoading();

      LoginResultEntity loginResultEntity = await event.login();

      yield LoginSuccess(loginResultEntity);
    }

    if (event is LoginPassWordEditEvent) {
      yield LoginPassWordEditState(!event.isObscure);
    }

    if (event is LoginUserNameEditChanagedEvent) {
      yield LoginUserNameEditChanagedState();
    }


    if (event is LoginCheckEvent) {
      yield LoginCheckState(event.nameError,event.passwordError);
    }


    if (event is LoginSaveUserChangedEvent) {
      yield LoginSaveUserChangedState(event.isSaveUser);
    }

    if ( event is LoginSaveUserEvent) {
      bool isSaveSuccess = await event.saveUser();

      yield LoginSaveUserEventState(isSaveSuccess);
    }

    if ( event is NullLoginEvent) {
      print('接受到了空状态事件');
      yield NullLoginEventState();
    }

  }
}
